package com.heyqing.topic.SortedArrayToBST_0108;

/**
 * ClassName:SortedArrayToBST
 * Package:com.heyqing.topic.SortedArrayToBST_0108
 * Description:
 *
 * @Date:2024/9/13
 * @Author:Heyqing
 */
public class SortedArrayToBST {
    public TreeNode sortedArrayToBST(int[] nums) {
        return achieveFunc(nums, 0, nums.length - 1);
    }

    private TreeNode achieveFunc(int[] nums, int left, int right) {
        if (left > right) return null;
        int mid = (left + right) / 2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = achieveFunc(nums, left, mid - 1);
        root.right = achieveFunc(nums, mid + 1, right);
        return root;
    }
}
